<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/1/19
 * Time: 9:23
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\api\model\mysql;

use app\common\model\TimeModel;

class MallGoodsSku extends TimeModel
{
    /**
     * @param int $goods_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMallGoodsSkuByValues(int $goods_id = 0): array
    {
        return $this
            ->where('goods_id', $goods_id)
            ->select()
            ->toArray();
    }

    /**
     * @param int $goods_id
     * @param string $specs_value
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMallGoodsSkuByList(int $goods_id = 0, string $specs_value = '')
    {
        return $this->field('id,image as logo,stock,price')
            ->where('goods_id', $goods_id)
            ->where('specs_value_ids', "like", "%$specs_value%")
            ->find();

    }

    /**
     * 查询指定商品规格
     * @param int $goods_id
     * @param int $id
     * @return mixed
     */
    public function getGoodsSkuById(int $goods_id =0, int $id=0){
        return $this->field('*')->where("id",$id)->where('goods_id',$goods_id)->find();
    }


    /**
     *
     * @param $value
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getGetSpecsValueIdsAttr($value){

        if(empty($value)){
            return '';
        }
        $ids = explode("-",$value);
        $res= (new MallSpecsValue())->where('id','in',$ids)
            ->select()->toArray();
        return array_column($res,'title');
    }
}